package com.xlp;

import com.xlp.additive.Additive;
import com.xlp.additive.MilkCap;
import com.xlp.drink.Coffee;
import com.xlp.drink.CustomDrink;
import com.xlp.drink.Drink;
import com.xlp.factory.AdditiveFactory;
import com.xlp.factory.DrinkFactory;
import com.xlp.order.DrinkOrder;

import java.util.*;

public class OrderExe {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("=============点单系统=============");
        List<Drink> drinks = new ArrayList<>();
        try {
            do {
                order(input, drinks);
                System.out.println("========1.继续点单=============");
                System.out.println("========2.退出系统=============");
                System.out.print("请输入相应的数字：");
                String s = input.nextLine().trim();
                if ("1".equals(s)){
                    order(input, new ArrayList<>());
                } else {
                    break;
                }
            } while (true);
        } catch (Exception e){
            System.out.println("-1");
        }
    }

    private static void order(Scanner input, List<Drink> drinks) {
        do {
            showDrinkSelectedInfo();
            String s = input.nextLine().trim();
            int flag = 0;
            do {
                if (!s.matches("^[1-7]$")){
                    System.out.println("输入错误，请重新输入！");
                    //showDrinkSelectedInfo();
                    s = input.nextLine().trim();
                } else {
                    flag = Integer.parseInt(s);
                }
            } while (flag == 0);
            //退出点饮料面板
            if (flag == 7) break;

            Drink drink = DrinkFactory.makeDrink(flag);

            System.out.println("是否选择添加料？（y/n）");
            boolean isSelected = "y".equalsIgnoreCase(input.nextLine().trim());
            if (isSelected){
                CustomDrink customDrink = new CustomDrink(drink);
                Set<String> hasSelected = new HashSet<>();

                showAdditiveInfo();

                for (; ; ) {
                    s = input.nextLine().trim();
                    if (!s.matches("^[1-5]$")) {
                        System.out.println("输入错误，请重新输入！");
                        continue;
                    } else {
                        flag = Integer.parseInt(s);
                    }

                    if (flag == 5) break;

                    if (hasSelected.contains(s)) {
                        System.out.println("改料已被添加，请重新选择！");
                    } else {
                        Additive additive = AdditiveFactory.makeAdditive(flag);
                        if (drink instanceof Coffee) {
                            if (!(additive instanceof MilkCap)) {
                                System.out.println("咖啡只能加奶盖，请重新选择！");
                                continue;
                            }
                        }
                        boolean hasAddMilkCap = customDrink.getAdditives()
                                .stream().anyMatch(item -> item instanceof MilkCap);
                        if (hasAddMilkCap) {
                            System.out.println("一杯饮料一次只能加一种奶盖，请重新选择！");
                            continue;
                        }

                        System.out.println("选择成功，请继续选择。");
                        hasSelected.add(s);
                        customDrink.addAdditive(additive);
                        showAdditiveInfo();
                    }
                }

                drink = customDrink;
            }
            drinks.add(drink);
        } while (drinks.size() < 10);
        System.out.println("正在打印订单详情：");
        DrinkOrder order = new DrinkOrder();
        order.setDrinks(drinks);
        System.out.println(order.getOrderDetail());
    }

    private static void showDrinkSelectedInfo(){
        System.out.println("======选择相应的序号购买饮料========");
        System.out.println("======1.椰果奶茶 10元=============");
        System.out.println("======2.西米奶茶 10元=============");
        System.out.println("======3.蜂蜜奶茶 12元=============");
        System.out.println("======4.杏仁奶茶 14元=============");
        System.out.println("======5.美式咖啡 11元=============");
        System.out.println("======6.拿铁咖啡 12元=============");
        System.out.println("======7.退出点单==================");
        System.out.print("请输入相应的数字：");
    }

    private static void showAdditiveInfo(){
        System.out.println("======选择相应的序号选择料======");
        System.out.println("======1.红豆 2元=============");
        System.out.println("======2.芋圆 2元=============");
        System.out.println("======3.奥利奥奶盖 4元========");
        System.out.println("======4.芝士奶盖 5元==========");
        System.out.println("======5.退出选择==============");
        System.out.print("请输入相应的数字：");
    }
}
